package com.appkubes.fintech.restful.application.rent.service.impl;

import com.appkubes.fintech.core.po.FintechRentBillHeader;
import com.appkubes.fintech.core.po.FintechRentBillLines;
import com.appkubes.fintech.core.po.FintechRentBillLinesExample;
import com.appkubes.fintech.core.pojo.FintechRentBillHeaderMapper;
import com.appkubes.fintech.core.pojo.FintechRentBillLinesMapper;
import com.appkubes.fintech.restful.application.rent.enums.RentBillEnums;
import com.appkubes.fintech.restful.application.rent.enums.RentOrderEnums;
import com.appkubes.fintech.restful.application.rent.service.IRentLinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class IRentLinesServiceImpl implements IRentLinesService {

    @Autowired
    private FintechRentBillLinesMapper linesMapper;
    @Autowired
    private FintechRentBillHeaderMapper billHeaderMapper;

    /**
     * @Date 2020/4/17 17:08
     * @Author XQC
     * @Description  检查是否有漏还期数
     */
    @Override
    public  Integer queryBeforPeriod(String billkey,Integer periods){
        FintechRentBillLinesExample example=new FintechRentBillLinesExample();
        FintechRentBillLinesExample.Criteria criteria=  example.createCriteria();
        criteria.andBillKeyEqualTo(billkey);
        criteria.andPeriodLessThan(periods);
        example.setOrderByClause(" period ");
        List<FintechRentBillLines> linesList=linesMapper.selectByExample(example);
        Integer period=null;
        for(FintechRentBillLines lines:linesList){
            if(Objects.equals(lines.getStatus(),RentBillEnums.还款中.getValue())
                    ||Objects.equals(lines.getStatus(),RentBillEnums.已逾期.getValue())||
                    Objects.equals(lines.getStatus(),RentBillEnums.未开始.getValue())) {
                period=lines.getPeriod();
                FintechRentBillHeader billHeader=billHeaderMapper.selectByPrimaryKey(lines.getBillKey());
                billHeader.setUnpayPeriods(lines.getPeriod());
                billHeader.setUnpayFineAmount(lines.getUnpayFineAmount());
                billHeader.setUnpayAmount(lines.getUnpayAmount());
                billHeader.setUnpayDate(lines.getPeriodEndDate());
                billHeaderMapper.updateByPrimaryKey(billHeader);
                break;
            }
        }

        return period;
    }
}
